package com.wanmait.spacesys.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.wanmait.spacesys.util.DbHelper;
import com.wanmait.spacesys.vo.Userinfo;
/**
 * 数据持久层:修改个人信息
 * @author 28298
 *
 */
public class UpdateUserinfoCenterDAO {
	//单一实例
	private UpdateUserinfoCenterDAO() {};
	private static UpdateUserinfoCenterDAO updateUserinfoCenterDAO=new UpdateUserinfoCenterDAO();
	public static UpdateUserinfoCenterDAO getUpdateUserinfoCenterDAO() {
		return updateUserinfoCenterDAO;
	}
	// 通过用户id获得用户的信息(登录用户)
	public Userinfo userinfoFindById(Integer userinfoId) {
		Userinfo userinfo = null;
		String sql = "SELECT * FROM userinfo WHERE id=?";
		Connection conn = DbHelper.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setInt(1, userinfoId);
			rs = ps.executeQuery();
			if (rs.next()) {
				userinfo = new Userinfo();
				userinfo.setId(userinfoId);
				userinfo.setUsername(rs.getString("username"));
				userinfo.setPassword(rs.getString("password"));
				userinfo.setNickname(rs.getString("nickname"));
				userinfo.setRegisteTime(rs.getTimestamp("registe_time"));
				userinfo.setHeaderPic(rs.getString("header_pic"));
				userinfo.setEnable(rs.getBoolean("enable"));
				userinfo.setPhone(rs.getString("phone"));
				userinfo.setReport(rs.getInt("report"));
				userinfo.setPraise(rs.getInt("praise"));
				userinfo.setPosting(rs.getInt("posting"));
				userinfo.setCollection(rs.getInt("collection"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			DbHelper.close(rs, ps, conn);
		}
		return userinfo;
	}
	
	
	//修改用户信息
	public void updateUserinfoFindAll(Userinfo userinfo) {
		String sql="Update userinfo set nickname=?,header_pic=?,phone=? where id=?";
		DbHelper.update(sql, userinfo.getNickname(),
							 userinfo.getHeaderPic(),
							 userinfo.getPhone(),
							 userinfo.getId());
	}
	
	

	//修改用户密码
	public void updatePassword(Userinfo userinfo) {
		String sql="Update userinfo set password=? where id=?";
		DbHelper.update(sql, userinfo.getPassword(),
							 userinfo.getId());
	}
	
		


}
